﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace AMPLibrary.Collections
{
    public class ExcludedMediaCollection : Collection<String>
    {
        //
        private static ExcludedMediaCollection collection;

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static ExcludedMediaCollection GetExcludedMediaCollection()
        {
            if (collection == null)
                collection = new ExcludedMediaCollection();

            return collection;
        }

        private ExcludedMediaCollection()
        {

        }

        public bool IsExcluded(string mediaPath)
        {
            var items = (from path in collection where path == mediaPath select path);

            if (items.Count() == 0)
                return false;
            else
                return true;
        }

        public void AddExcludedItem(string mediaPath)
        {
            var items = (from path in collection where path == mediaPath select path);

            if (items.Count() == 0)
                this.AddItem(mediaPath);
        }

        public void RemoveExcludedItem(string mediaPath)
        {
            Collection<string> items = new Collection<string>(from path in collection where path == mediaPath select path);

            foreach (string media in items)
                this.RemoveItem(media);
        }        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool Serialize(string path)
        {
            Stream stream = null; 
            BinaryFormatter bFormatter = new BinaryFormatter();

            try
            {
                if (File.Exists(path))
                {
                    try
                    {
                        File.Delete(path);
                    }
                    catch
                    {
                    }
                }

                stream = File.Open(path, FileMode.Create);

                System.Collections.ArrayList al = new System.Collections.ArrayList();

                foreach (string item in collection)
                    al.Add(item);

                bFormatter.Serialize(stream, al);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }

            return true;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public bool Deserialize(string path)
        {
            if (collection.Count > 0)
                return true;

            Stream stream = null;
            BinaryFormatter bFormatter = new BinaryFormatter();

            try
            {
                stream = File.Open(path, FileMode.Open);
                System.Collections.ArrayList al = (System.Collections.ArrayList)bFormatter.Deserialize(stream);

                foreach (string item in al)
                    collection.AddItem(item);
            }
            catch
            {
                return false;
            }
            finally
            {
                if(stream != null)
                    stream.Close();
            }

            return true;
        }
    }
}
